//
//  AppStorePraise.m
//  com.yjwh.yj
//
//  Created by ferver7 on 2018/12/27.
//  Copyright © 2018 ferver7. All rights reserved.
//

#import "AppStorePraise.h"
#import <StoreKit/SKStoreReviewController.h>

@implementation AppStorePraise
#pragma mark   ------------- 邀请用户去appstore评价  --------------
+(void)QuestionGetAppInfoForCommentFromVC:(UIViewController *)fromVC{
    if (!UserDefaultsGetSynchronize(@"firstAppPraise")) {
        UserDefaultsSynchronize(@1, @"firstAppPraise");
    }else{
        UserDefaultsSynchronize(@0, @"firstAppPraise");
    }
    //去appstore评分  //当前版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue];
    //userDefaults里的天数
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger usertheDays = [[userDefaults objectForKey:@"theDays"] intValue];
    
    NSLog(@"appVersion %f udtheDays %ld",appVersion,(long)usertheDays);
    //userDefaults里的版本号
    float udAppVersion = [[userDefaults objectForKey:@"appVersion"] floatValue];
    //userDefaults里用户上次的选项
    NSInteger udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue];
    //时间戳的天数
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    NSInteger daySeconds = 24 * 60 * 60;
    NSInteger currentDays = interval / daySeconds;
    NSLog(@"appVersion %@",[userDefaults objectForKey:@"appVersion"]);
    NSLog(@"userOptChoose %@",[userDefaults objectForKey:@"userOptChoose"]);
    NSLog(@"theDays %@",[userDefaults objectForKey:@"theDays"]);
    NSLog(@"udAppVersion %f",udAppVersion);
    //版本升级之后的处理,全部规则清空,开始弹窗
    NSLog(@"theDays%ld udtheDays%ld",(long)currentDays,(long)usertheDays);
    NSNumber *appPraise = UserDefaultsGetSynchronize(@"firstAppPraise");
    if (appPraise.integerValue !=1 && udAppVersion && appVersion>udAppVersion) {
        [userDefaults removeObjectForKey:@"theDays"];
        [userDefaults removeObjectForKey:@"appVersion"];
        [userDefaults removeObjectForKey:@"userOptChoose"];
        [userDefaults removeObjectForKey:@"firstAppPraise"];
        NSLog(@"版本升级之后的处理,清空存储信息,开始弹窗");
        [self alertUserCommentViewFromVC:fromVC];
    }
    //1,从来没弹出过的
    //2,用户选择不好用我要提意见，7天之后再弹出
    //3,用户选择残忍的拒绝后，7天内，每过1天会弹一次
    else if (!udUserChoose ||
             (udUserChoose==2 && currentDays-usertheDays>7) ||
             (udUserChoose==3 && currentDays-usertheDays<=7 && currentDays-usertheDays>0) )
    {
        if (appPraise.integerValue !=1) {
            [self alertUserCommentViewFromVC:fromVC];
        }
    }
    
}
#pragma mark   ------------- 跳槽alerview --------------
+(void)alertUserCommentViewFromVC:(UIViewController *)fromVC{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //当前时间戳的天数
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    NSInteger daySeconds = 24 * 60 * 60;
    NSInteger currentDays = interval / daySeconds;
    //当前版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue];
    //userDefaults里版本号
    float udAppVersion = [[userDefaults objectForKey:@"appVersion"] floatValue];
    //userDefaults里用户选择项目
    NSInteger udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue];
    //userDefaults里用户天数
    //当前版本比userDefaults里版本号高
    if (appVersion>udAppVersion) {
        [userDefaults setObject:[NSString stringWithFormat:@"%f",appVersion] forKey:@"appVersion"];
    }
    BOOL need = NO;
    if ([mSystemVersion floatValue] > 10.3 && need) {
        //影响提醒弹窗
        if([SKStoreReviewController respondsToSelector:@selector(requestReview)]) {// iOS 10.3 以上支持
            //防止键盘遮挡
            [[UIApplication sharedApplication].keyWindow endEditing:YES];
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新，
                //                [AxcUI_Toast AxcUI_showCenterWithText:@"支付成功"];
                [SKStoreReviewController requestReview];
            });
        }
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请给个好评吧"message:@"如果您喜欢域鉴，请给我们个好评，也可以直接反馈意见给我们"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *praise  = [UIAlertAction actionWithTitle:@"给个好评" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
            NSLog(@"点击给个好评");
            [userDefaults setObject:@"1" forKey:@"userOptChoose"];
            [userDefaults setObject:[NSString stringWithFormat:@"%ld",currentDays] forKey:@"theDays"];
            [self StoreComment];
        }];
        UIAlertAction *opinion = [UIAlertAction actionWithTitle:@"不好用，我要提意见" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
            NSLog(@"不好用，我要提意见");
            [userDefaults setObject:@"2" forKey:@"userOptChoose"];
            [userDefaults setObject:[NSString stringWithFormat:@"%ld",currentDays] forKey:@"theDays"];
            //可以调转到程序内部的反馈意见页面
        }];
        UIAlertAction *reject = [UIAlertAction actionWithTitle:@"残忍的拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
            NSLog(@"残忍的拒绝 udUserChoose%ld",(long)udUserChoose);
            [userDefaults setObject:@"3" forKey:@"userOptChoose"];
            [userDefaults setObject:[NSString stringWithFormat:@"%ld",currentDays] forKey:@"theDays"];
        }];
        [alert addAction:praise];
        [alert addAction:opinion];
        [alert addAction:reject];
        [fromVC presentViewController:alert animated:YES completion:nil];
    }
}
#pragma mark   ------------- 跳转到appstore评论 --------------
+(void)StoreComment{
    NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",@"1443704359"];
    NSURL * url = [NSURL URLWithString:str];
    [[UIApplication sharedApplication] openURL:url];
}
+(void)isOpenLocalNotificationFromVC:(UIViewController *)fromVC;
{
   NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([[userDefaults objectForKey:@"noPushShow"] intValue] == 1) {//用户选择不再提醒
        return;
    }
    BOOL isTurnOnPush = YES;
    if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
        if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != UIRemoteNotificationTypeNone) {
            isTurnOnPush = YES;
        } else {
            isTurnOnPush = NO;
        }
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == 0) {
            isTurnOnPush = NO;
        } else {
            isTurnOnPush = YES;
        }
    }
    if (!isTurnOnPush) {
        //当前时间戳的天数
        NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
        NSInteger daySeconds = 24 * 60 * 60;
        NSInteger currentDays = interval / daySeconds;
        NSInteger usertheDays = [[userDefaults objectForKey:@"pushTheDays"] intValue];
        int k= rand()%(5);
        if (usertheDays > 0 && currentDays-usertheDays-(k+2)> 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开启推送"message:@"为了您更好的体验我们的服务,建议您去开启推送,实时接收动态消息!"preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *praise  = [UIAlertAction actionWithTitle:@"不再提醒" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
                NSLog(@"点击给个好评");
                [userDefaults setObject:@"1" forKey:@"noPushShow"];
            }];
            UIAlertAction *opinion = [UIAlertAction actionWithTitle:@"再看看" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
                NSLog(@"不好用，我要提意见");
                //可以调转到程序内部的反馈意见页面
            }];
            UIAlertAction *reject = [UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){
                [userDefaults setObject:[NSNumber numberWithLong:currentDays] forKey:@"pushTheDays"];
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }];
            [alert addAction:praise];
            [alert addAction:opinion];
            [alert addAction:reject];
            [fromVC presentViewController:alert animated:YES completion:nil];
        }else if (usertheDays == 0){
            [userDefaults setObject:[NSNumber numberWithLong:currentDays] forKey:@"pushTheDays"];
        }else{
            
        }
    }
}
@end
